package dao.impl;

import core.BaseDao;
import dao.PermissionDao;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import pojo.Department;
import pojo.Permission;
import util.MyMapListHandler;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PermissionDaoImpl extends BaseDao implements PermissionDao {

    /**
     * 通过部门编号查找权限
     * @param permissionCode
     * @return
     */
    @Override
    public Permission findPermissionByCode(String permissionCode) {
        try {
            sql = "SELECT * FROM permission WHERE permission_code = ?";
            return queryRunner.query(sql, new BeanHandler<>(Permission.class, rowProcessor), permissionCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 修改职位的权限
     * @param departmentCode
     * @param departmentPermission
     * @return
     */
    @Override
    public Integer editPermission(String departmentCode, String departmentPermission) {
        try {
            sql = "UPDATE department SET department_permission = ? WHERE department_code = ?";
            return queryRunner.update(sql, departmentPermission, departmentCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据职位查找权限
     * @param departmentCode
     * @return
     */
    @Override
    public Department findPermissionByDC(String departmentCode) {
        try {
            sql = "SELECT * FROM department WHERE department_code = ?";
            return queryRunner.query(sql, new BeanHandler<>(Department.class, rowProcessor), departmentCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 按条件展示所有权限的方法
     * @param permission
     * @return
     */
    @Override
    public List<Permission> listPermissionByParam(Permission permission) {
        try {
            List<Permission> permissionList = new ArrayList<>();
            List<Map<String, Object>> mapList = null;
            sql = "SELECT * FROM permission";
            if (permission.getPermissionName() != null && !permission.getPermissionName().isEmpty()){
                sql += " AND permission_name LIKE ?";
                mapList = queryRunner.query(sql, new MyMapListHandler(), "%"+permission.getPermissionName()+"%");
            }else{
                mapList = queryRunner.query(sql, new MyMapListHandler());
            }
            for (Map<String, Object> map : mapList) {
                Permission permission1 = new Permission();
                BeanUtils.populate(permission1, map);
                permissionList.add(permission1);
            }
            return permissionList;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**
     * 按条件展示所有权限的个数
     * @param permission
     * @return
     */
    @Override
    public Integer countListPermissionByParam(Permission permission) {
        try {
            sql = "SELECT count(1) FROM permission";
            if (permission.getPermissionName() != null && !permission.getPermissionName().isEmpty()){
                sql += " AND permission_name LIKE ?";
                return Integer.valueOf(queryRunner.query(sql, new ScalarHandler<>(), "%"+permission.getPermissionName()+"%").toString());
            }else{
                return Integer.valueOf(queryRunner.query(sql, new ScalarHandler<>()).toString());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
